<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <a-form-item label="支付通道" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['default', { rules: [{ required: true }] }]">
            <a-radio value="smpay">SM跨境支付</a-radio>
            <a-radio value="other">其他支付</a-radio>
          </a-radio-group>
          <div class="form-item-help">
            <small style="margin-right: 6px;">支付通道说明:</small>
            <a v-show="form.getFieldValue('default') === 'smpay'"
               href="#"
               target="_blank">
              SM跨境支付通道
            </a>
            <a v-show="form.getFieldValue('default') === 'other'"
               href="#"
               target="_blank">
              其他支付通道，未开放
            </a>
          </div>
        </a-form-item>
        <!-- 支付通道配置 -->
        <div v-show="form.getFieldValue('default') === 'smpay'">
          <a-form-item label="channelId" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.channelId`]" />
          </a-form-item>
          <a-form-item
            label="merName"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            required
          >
            <a-input v-decorator="[`engine.smpay.merName`]" />
          </a-form-item>
          <a-form-item label="merchId" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.merchId`]" />
            <div class="form-item-help">
              <small style="margin-right: 6px;">币种说明:</small><br>
              美金填ZK010USD<br>
              欧元填ZK010EUR<br>
              港币填ZK010HKD<br>
              日元填ZK010JPY<br>
              韩币填ZK010KRW
            </div>
          </a-form-item>
          <a-form-item label="oemsys_branchCode" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.oemsys_branchCode`]" />
          </a-form-item>
          <a-form-item label="oemsys_url" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.oemsys_url`]" />
          </a-form-item>
          <a-form-item label="域名" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.domain`]" />
          </a-form-item>
          <a-form-item label="notify" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smpay.notify`]" />
          </a-form-item>
          <a-form-item label="oemsys_privatekey" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-decorator="[`engine.smpay.oemsys_privatekey`]" />
          </a-form-item>
        </div>
        <!--        美圣融云-->
        <div v-show="form.getFieldValue('default') === 'other'">
          <a-form-item
            label="channelId"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            required
          >
            <a-input v-decorator="[`engine.other.channelId`]" />
          </a-form-item>
          <a-form-item label="merName" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.other.merName`]" />
          </a-form-item>
        </div>
        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { isEmpty } from '@/utils/util'
import * as Api from '@/api/setting/store'
import SettingSmsSceneEnum from '@/common/enum/setting/sms/Scene'

export default {
  data () {
    return {
      SettingSmsSceneEnum,
      // 当前设置项的key
      key: 'pay',
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {}
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail(this.key)
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form } = this
      !isEmpty(form.getFieldsValue()) && $nextTick(() => {
        /* const scene = {}
        for (const index in record.scene) {
          scene[index] = pick(record.scene[index], ['isEnable', 'channelId', 'acceptPhone', 'content'])
        } */
        form.setFieldsValue({
          default: record.default,
          engine: record.engine
        })
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.isLoading = true
      Api.update(this.key, { form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5)
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  margin-bottom: 10px;
}
/deep/.ant-form-item-control {
  padding-left: 10px;

  .ant-form-item-control {
    padding-left: 0;
  }
}
.ant-divider {
  margin-top: 50px !important;
}
.info{color:#666;}
</style>
